package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.Criteria;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.BlogCategoryMapper;
import com.wontube.smr.dao.mapper.web.BlogMapper;
import com.wontube.smr.dao.mapper.web.BlogTagMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.Blog;
import com.wontube.smr.web.client.model.po.BlogCategory;
import com.wontube.smr.web.client.model.po.BlogTag;
import com.wontube.smr.web.client.model.vo.BlogItems;

/**
 * @author tanggf(2012-5-25)
 *
 */
public class BlogDao {
	/**
	 * 查询blog
	 * @param status
	 * @param pageInfo
	 * @return
	 */
	public static BlogItems queryBlogs(Integer status, String category, PagenateInfo pageInfo){
		BlogItems items = new BlogItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			Criteria cria = null;
			if(status != null){
				cria = ex.or();
				cria.addCriterion("status=", status);
			}
			if(category != null){
				if(cria == null){
					cria = ex.or();
				}
				cria.addCriterion("category=", category);
			}
			
			ex.setOrderByClause("created desc");
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			List<Blog> blogs = mapper.selectByExample(ex, new RowBounds(pageInfo.getOffset(), pageInfo.getLimit()));
			items.setItems(blogs);
			
			int total = mapper.countByExample(ex);
			items.setTotalCount(total);
			
		} finally {
			session.close();
		}
		return items;
	}
	
	
	public static BlogItems listTypeBlogs(PagenateInfo pageInfo,String type,String typeName){
		BlogItems items = new BlogItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			if(type.equals("category")){
				ex.or().addCriterion("status=", Blog.STATUS_RELEASED).addCriterion("category like ","%"+typeName+"%");
			}else if(type.equals("tag")){
				ex.or().addCriterion("status=", Blog.STATUS_RELEASED).addCriterion("tags like ","%"+typeName+"%");
			}else{
				return null;
			}
			ex.setOrderByClause("created desc");
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			List<Blog> blogs = mapper.selectByExample(ex, new RowBounds(pageInfo.getOffset(), pageInfo.getLimit()));
			items.setItems(blogs);
			
			int total = mapper.countByExample(ex);
			items.setTotalCount(total);
			
		} finally {
			session.close();
		}
		return items;
	}
	
	public static Blog getBlogByPk(String id){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			return mapper.selectByPrimaryKey(id);
		} finally {
			session.close();
		}
	}
	
	public static boolean addOrUpdateBlog(Blog blog){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			Blog tmp = mapper.selectByPrimaryKey(blog.getId());
			int aff = 0;
			if(tmp != null){
				aff = mapper.updateByPrimaryKeySelective(blog);
			}else{
				aff = mapper.insert(blog);				
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean updateBlogStatus(String id, Integer status){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			Blog blog = new Blog();
			blog.setId(id);
			blog.setStatus(status);
			int aff = mapper.updateByPrimaryKeySelective(blog);
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static Blog getReleasedBlogDetail(String urlTitle){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("url_title=", urlTitle)
					.addCriterion("status=", Blog.STATUS_RELEASED);
			List<Blog> blogs = mapper.selectByExample(ex);
			if(blogs.size() > 0){
				return blogs.get(0);
			}
			return null;
		} finally {
			session.close();
		}
	}
	
	public static BlogItems listReleasedBlogs(PagenateInfo pageInfo){
		BlogItems items = new BlogItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogMapper mapper = session.getMapper(BlogMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("status=", Blog.STATUS_RELEASED);
			ex.setOrderByClause("created desc");
			List<Blog> blogs = mapper.selectByExample(ex, new RowBounds(pageInfo.getOffset(), pageInfo.getLimit()));
			items.setItems(blogs);
			
			int total = mapper.countByExample(ex);
			items.setTotalCount(total);
			return items;
		} finally {
			session.close();
		}
	}
	
	//=====
	public static boolean addBlogCategory(BlogCategory cate){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogCategoryMapper mapper = session.getMapper(BlogCategoryMapper.class);
			int aff = mapper.insert(cate);
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean deleteBlogCategory(String id){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			BlogCategoryMapper mapper = session.getMapper(BlogCategoryMapper.class);
			int aff = mapper.deleteByPrimaryKey(id);
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static List<BlogCategory> listAllBlogCategories(){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("sort_index asc");
			BlogCategoryMapper mapper = session.getMapper(BlogCategoryMapper.class);
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	public static List<BlogCategory> listAllCatAndCount(){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			//ex.setOrderByClause("sort_index asc");
			BlogCategoryMapper mapper = session.getMapper(BlogCategoryMapper.class);
			return mapper.listAllCatAndCount(ex);
		} finally {
			session.close();
		}
	}
	
	//=====
		public static int addBlogTag(BlogTag tag){
			SqlSession session = DefaultDBHelper.getSqlSession(true);
			try {
				BlogTagMapper mapper = session.getMapper(BlogTagMapper.class);
				return mapper.insert(tag);			
			} finally {
				session.close();
			}
		}
		
		public static int deleteBlogTag(String id){
			SqlSession session = DefaultDBHelper.getSqlSession(true);
			try {
				BlogTagMapper mapper = session.getMapper(BlogTagMapper.class);
				return mapper.deleteByPrimaryKey(id);				
			} finally {
				session.close();
			}
		}
		
		public static List<BlogTag> listAllBlogTags(){
			SqlSession session = DefaultDBHelper.getSqlSession(true);
			try {
				QueryExample ex = new QueryExample();
				ex.setOrderByClause("sort_index asc");
				BlogTagMapper mapper = session.getMapper(BlogTagMapper.class);
				return mapper.selectByExample(ex);
			} finally {
				session.close();
			}
		}
		
		public static List<BlogTag> listAllBlogTagsPage(PagenateInfo pageInfo){
			SqlSession session = DefaultDBHelper.getSqlSession(true);
			try {
				QueryExample ex = new QueryExample();
				ex.setOrderByClause("sort_index desc");
				BlogTagMapper mapper = session.getMapper(BlogTagMapper.class);
				return mapper.selectByExample(ex, new RowBounds(pageInfo.getOffset(), pageInfo.getLimit()));
			} finally {
				session.close();
			}
		}
		
		public static List<BlogTag> listAllBlogTagsPage(){
			SqlSession session = DefaultDBHelper.getSqlSession(true);
			try {
				QueryExample ex = new QueryExample();
				ex.setOrderByClause("sort_index desc");
				BlogTagMapper mapper = session.getMapper(BlogTagMapper.class);
				return mapper.selectByExample(ex);
			} finally {
				session.close();
			}
		}
}
